.TH std::ranges::views::transform,std::ranges::transform_view 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::ranges::views::transform,std::ranges::transform_view \- std::ranges::views::transform,std::ranges::transform_view

.SH Synopsis
   Defined in header <ranges>
   template< ranges::input_range V,

             std::copy_constructible F >
       requires ranges::view<V> &&
                std::is_object_v<F> &&
                std::regular_invocable<F&,                                \fI(since C++20)\fP
   ranges::range_reference_t<V>> &&                                       (until C++23)
                /* invoke_result_t<F&, range_reference_t<V>>& is
   a valid type */
   class transform_view

       : public ranges::view_interface<transform_view<V, F>>
   template< ranges::input_range V,

             std::move_constructible F >
       requires ranges::view<V> &&
                std::is_object_v<F> &&
                std::regular_invocable<F&,                                (since C++23)
   ranges::range_reference_t<V>> &&                               \fB(1)\fP
                /* invoke_result_t<F&, range_reference_t<V>>& is
   a valid type */
   class transform_view

       : public ranges::view_interface<transform_view<V, F>>
   namespace views {

       inline constexpr /*unspecified*/ transform =                   \fB(2)\fP \fI(since C++20)\fP
   /*unspecified*/;

   }
   Call signature
   template< ranges::viewable_range R, class F >

       requires /* see below */                                           \fI(since C++20)\fP

   constexpr ranges::view auto transform( R&& r, F&& fun );
   template< class F >                                                    \fI(since C++20)\fP
   constexpr /*range adaptor closure*/ transform( F&& fun );

   1) A range adaptor that represents view of an underlying sequence after applying a
   transformation function to each element.
   2) RangeAdaptorObject. The expression views::transform(e, f) is
   expression-equivalent to transform_view(e, f) for any suitable subexpressions e and
   f.

   transform_view models the concepts random_access_range, bidirectional_range,
   forward_range, input_range, common_range, and sized_range when the underlying view V
   models respective concepts.

.SH Member functions

   constructor   constructs a transform_view
   (C++20)       \fI(public member function)\fP
   base          returns a copy of the underlying (adapted) view
   (C++20)       \fI(public member function)\fP
   begin         returns an iterator to the beginning
   (C++20)       \fI(public member function)\fP
   end           returns an iterator or a sentinel to the end
   (C++20)       \fI(public member function)\fP
   size          returns the number of elements. Provided only if the underlying
   (C++20)       (adapted) range satisfies sized_range.
                 \fI(public member function)\fP
         Inherited from std::ranges::view_interface
   empty         returns whether the derived view is empty. Provided if it satisfies
   (C++20)       sized_range or forward_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   cbegin        returns a constant iterator to the beginning of the range.
   (C++23)       \fI(public member function of std::ranges::view_interface<D>)\fP
   cend          returns a sentinel for the constant iterator of the range.
   (C++23)       \fI(public member function of std::ranges::view_interface<D>)\fP
   operator bool returns whether the derived view is not empty. Provided if
   (C++20)       ranges::empty is applicable to it.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   front         returns the first element in the derived view. Provided if it
   (C++20)       satisfies forward_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   back          returns the last element in the derived view. Provided if it satisfies
   (C++20)       bidirectional_range and common_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP
   operator[]    returns the n^th element in the derived view. Provided if it satisfies
   (C++20)       random_access_range.
                 \fI(public member function of std::ranges::view_interface<D>)\fP

   Deduction guides

   Nested classes

   iterator the iterator type
   (C++20)  (exposition-only member class template*)
   sentinel the sentinel type
   (C++20)  (exposition-only member class template*)

.SH Example


// Run this code

 #include <algorithm>
 #include <cstdio>
 #include <iterator>
 #include <ranges>
 #include <string>

 char rot13a(const char x, const char a)
 {
     return a + (x - a + 13) % 26;
 }

 char rot13(const char x)
 {
     if ('Z' >= x and x >= 'A')
         return rot13a(x, 'A');

     if ('z' >= x and x >= 'a')
         return rot13a(x, 'a');

     return x;
 }

 int main()
 {
     auto show = [](const unsigned char x) { std::putchar(x); };

     std::string in{"cppreference.com\\n"};
     std::ranges::for_each(in, show);
     std::ranges::for_each(in | std::views::transform(rot13), show);

     std::string out;
     std::ranges::copy(std::views::transform(in, rot13), std::back_inserter(out));
     std::ranges::for_each(out, show);
     std::ranges::for_each(out | std::views::transform(rot13), show);
 }

.SH Output:

 cppreference.com
 pccersrerapr.pbz
 pccersrerapr.pbz
 cppreference.com

.SH See also

   ranges::transform applies a function to a range of elements
   (C++20)           (niebloid)
